/*
 * SteepTimer - A timer designed for tea fanatics
 * Copyright (C) 2011  Wesley Crossman
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "TeaListWidget.h"
#include <QMouseEvent>
#include <QFont>
#include <QDebug>

TeaListWidget::TeaListWidget(QWidget *parent) :
            QListWidget(parent),
            m_running(-1),
            m_lastRunning(-1) {
}

void TeaListWidget::mouseDoubleClickEvent(QMouseEvent *event) {
    emit doubleClickedAnywhere(indexAt(event->pos()));
    QListWidget::mouseDoubleClickEvent(event);
}

void TeaListWidget::updateTimerFonts() {
    int next = (m_lastRunning+1) % count();
    for (int a=0;a<count();++a) {
        QFont font;
        font.setUnderline(a == next && m_running == -1);
        font.setBold(a == m_running);
        item(a)->setFont(font);
    }
}

void TeaListWidget::selectTimer(int row) {
    deselectRunningTimer(true);
    m_running = row;
    updateTimerFonts();
    emit runningTimerIndexChanged(m_running);
}

void TeaListWidget::deselectRunningTimer(bool quiet) {
    if (m_running >= 0) {
        m_lastRunning = m_running;
        m_running = -1;
        updateTimerFonts();
        if (!quiet) emit runningTimerIndexChanged(m_running);
    }
}
